package me.siphone;

import android.app.Application;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.hjq.http.EasyConfig;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.config.IRequestServer;
import com.hjq.http.model.HttpHeaders;
import com.hjq.http.model.HttpParams;
import com.hjq.http.request.HttpRequest;
import com.tencent.mmkv.MMKV;
import me.siphone.http.model.RequestHandler;
import me.siphone.http.server.ReleaseServer;
import okhttp3.OkHttpClient;
import org.linphone.BuildConfig;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        MMKV.initialize(this);

        // 网络请求框架初始化
        IRequestServer server = new ReleaseServer();

        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

        EasyConfig.with(okHttpClient)
                // 是否打印日志
                .setLogEnabled(BuildConfig.DEBUG)
                // 设置服务器配置（必须设置）
                .setServer(server)
                // 设置请求处理策略（必须设置）
                .setHandler(new RequestHandler(this))
                // 设置请求参数拦截器
                .setInterceptor(
                        new IRequestInterceptor() {
                            @Override
                            public void interceptArguments(
                                    @NonNull HttpRequest<?> httpRequest,
                                    @NonNull HttpParams params,
                                    @NonNull HttpHeaders headers) {
                                String token = MMKV.defaultMMKV().getString(Constants.TOKEN, "");
                                if (!TextUtils.isEmpty(token)) {
                                    headers.put("Authorization", "Bearer " + token);
                                }
                            }
                        })

                // 设置请求重试次数
                .setRetryCount(1)
                // 设置请求重试时间
                .setRetryTime(2000)
                // 添加全局请求头
                // .addHeader("date", "20191030")
                .into();
    }
}
